package com.daxia.ogmall.service;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.daxia.ogmall.dao.SpecificationDAO;
import com.daxia.ogmall.dto.SpecificationDTO;
import com.daxia.ogmall.model.Specification;
import com.daxia.ogmall.support.Page;
import com.daxia.ogmall.util.BeanMapper;

@Service
public class SpecificationService {
	
	@Autowired
	private SpecificationDAO specificationDAO;
	
	public List<SpecificationDTO> find(SpecificationDTO query, Page page) {
		List<Specification> models = specificationDAO.find(query, page);
		List<SpecificationDTO> dtos = toDTOs(models);
		return dtos;
	}
	
	public List<SpecificationDTO> findAll() {
		return this.find(new SpecificationDTO(), null);
	}
	
	public List<SpecificationDTO> find(int count) {
		Page page = new Page();
		page.setPage(1);
		page.setPageSize(count);
		return this.find(new SpecificationDTO(), page);
	}
	
	public Long create(SpecificationDTO dto) {
		Specification model = new Specification();
		toModel(model, dto);
		return specificationDAO.create(model);
	}
	
	public SpecificationDTO load(Long id) {
	    Specification model = specificationDAO.load(id);
	    return toDTO(model);
    }

	public void updateAllFields(SpecificationDTO dto) {
		Specification model = specificationDAO.load(dto.getId());
		toModel(model, dto);
		specificationDAO.update(model);
    }

	public void deleteByIds(Long[] ids) {
		if (ids != null) {
			for (Long id : ids) {
				specificationDAO.deleteById(id);
			}
		}
    }
	
	public void deleteById(Long id) {
		this.deleteByIds(new Long[] {id});
	}

	public SpecificationDTO findOne(SpecificationDTO query) {
		Specification model = specificationDAO.findOne(query);
		return toDTO(model);
	}
	
	private List<SpecificationDTO> toDTOs(List<Specification> models) {
		if (CollectionUtils.isEmpty(models)) {
			return new ArrayList<SpecificationDTO>(0);
		}
		List<SpecificationDTO> dtos = new ArrayList<SpecificationDTO>(models.size());
		for (Specification model : models) {
	        SpecificationDTO dto = toDTO(model);
	        dtos.add(dto);
        }
	    return dtos;
    }
    
    public SpecificationDTO toDTO(Specification model) {
		if (model == null) {
			return null;
		}
		SpecificationDTO dto = BeanMapper.map(model, SpecificationDTO.class);
		
		return dto;
	}
	
	private void toModel(Specification model, SpecificationDTO dto) {
		BeanMapper.copy(dto, model);
    }
	
	@SuppressWarnings("unused")
	private List<Specification> toModels(List<SpecificationDTO> dtos) {
		if (CollectionUtils.isEmpty(dtos)) {
			return new ArrayList<Specification>(0);
		}
		List<Specification> models = new ArrayList<Specification>(dtos.size());
		for (SpecificationDTO dto : dtos) {
	        Specification model = new Specification();
	        toModel(model, dto);
	        models.add(model);
        }
		return models;
	}
	
	public Integer findSeq(SpecificationDTO dto) {
		Specification specification = specificationDAO.findSeq(dto);
		return specification.getSeq();
	}

	public SpecificationDTO findByOgCode(String ogCode) {
		if (StringUtils.isBlank(ogCode)) {
			return null;
		}
		SpecificationDTO query = new SpecificationDTO();
		query.setOgCode(ogCode);
		return this.findOne(query);
	}
}
